package cn.itcast.service.system.impl;

import cn.itcast.dao.system.RoleDao;
import cn.itcast.domain.system.Role;
import cn.itcast.service.system.RoleService;

import com.alibaba.excel.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    //查询
    @Override
    public PageInfo findAll(int page,int size,String companyId) {
        PageHelper.startPage(page,size);

        List<Role> list = roleDao.findAll(companyId);
        return new PageInfo(list);
    }
    //添加
    @Override
    public void save(Role role) {
            role.setId(UUID.randomUUID().toString());
            roleDao.save(role);
    }

    @Override
    public Role findById(String id) {
        Role role = roleDao.findById(id);
        return role;
    }

    @Override
    public void update(Role role) {
        roleDao.update(role);
    }

    @Override
    public void delete(String id) {
        roleDao.delete(id);
    }

    //查询所有角色不带分页
    @Override
    public List findAll(String companyId) {
        List<Role> list = roleDao.findAll(companyId);
        return list;
    }

    //根据id查角色权限
    @Override
    public List findRoleModule(String roleId) {

        List<String> list = roleDao.findRoleModule(roleId);

        return list;
    }

    @Override
    public void saveRoleModule(String roleid, String moduleIds) {
        roleDao.deleteRoleModule(roleid);
        String[] mids = moduleIds.split(",");
        for (String mid : mids) {
            roleDao.saveRoleModule(roleid,mid);
        }


    }
}
